<template>
  <div class="new-activity" :class="[{'empty':showEmpty}]">
      <template v-if="showSkeleton">
          <s-activity ></s-activity>
      </template>
      <template v-else>
          <ul v-infinite-scroll="requestData" infinite-scroll-disabled="cellLoading" infinite-scroll-distance="0" infinite-scroll-immediate-check="false">
              <router-link tag="li" v-for="item in activityData" :to=" 'information/' + item.id">
                <v-card :data="item"></v-card>
              </router-link>
          </ul>
          <div v-if="showEmpty" class="div-empty">
              <span class="iconfont icon-meiyougengduole"></span>
              <p class="empty-tip">暂无最新资讯~</p> 
          </div>
      </template>
      
      
  </div>
</template>

<script>
import { mapState } from "vuex";
import vCard from "./card.vue";
import Vue from "vue";
import { InfiniteScroll } from "mint-ui";
Vue.use(InfiniteScroll);
import sActivity from "../../skeletons/sActivity.vue";

export default {
  name: "new-activity",
  data() {
    return {
      activityData: [],
      showEmpty: false,
      cellLoading: true,
      curPage: 0,
      pageNum: 10,
      existIdArr: [],
      isLoadDb: false,
      showSkeleton:true,
      clearId:-1000
    };
  },
  components: { vCard,sActivity },
  activated() {},
  mounted() {
    this.clearId=setTimeout(()=>{
      clearTimeout(this.clearId);
      this.clearId=-1000;
      if(this.isLoadDb) this.showSkeleton=false;
    },300);
    this.requestData(false);
  },
  methods: {
    requestData(infinite = true) {
      this.$store.dispatch("toggleLoading", true);
      this.$http
        .post(this.path + "store/Goods/goods_list", {
          type: "4",
          page: this.curPage,
          storeid: this.storeid
        })
        .then(response => {
          this.$store.dispatch("toggleLoading", false);
          if (infinite && this.page == 0) return;
          if (infinite && this.cellLoading) return;
          this.cellLoading = true;
          if (!this.isLoadDb){
            this.isLoadDb=true;
            //如果过了400ms，移除骨架页面
            if(this.clearId==-1000) this.showSkeleton=false;
          }
          let serverData = response.body.result;
          if (!serverData || serverData.length == 0) {
            //数据为空
            this.cellLoading = true;
            if (this.curPage == 0){
              this.showEmpty = true;
            }
          } else {
            //有数据
            if (serverData.length % 10 == 0) {
              //满一页数据
              this.curPage++;
              this.cellLoading = false;
            } else {
              //不满一页
              this.cellLoading = true;
            }
            let detailsArr = this.analysisData(serverData);
            for (let i = 0; i < detailsArr.length; i++) {
              let orderid = detailsArr[i].id;
              if (this.existIdArr.indexOf(orderid) >= 0) continue;
              this.activityData.push(detailsArr[i]);
              this.existIdArr.push(orderid);
            }
          }
        })
        .catch(response => {
          console.log(response);
          this.$store.dispatch("toggleLoading", false);
        });
    },
    analysisData(data) {
      let detailsArr = [];
      data.forEach((val, index, arr) => {
        detailsArr.push({
          id: val.id,
          type: "1",
          imgUrl: val.icon,
          itemName: val.gname,
          price: val.price
        });
      });
      return detailsArr;
    }
  },
  computed: {
    ...mapState(["openid", "path", "userid", "storeid",'soursePath'])
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.new-activity {
  width: 100%;
  flex: 1;
  padding-bottom: 0.43rem;
  background-color: #efeff0;

  &.empty {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
  }

  ul {
    padding: 0.07rem 0.09rem 0 0.09rem;

    li {
      width: 100%;
      height: 1.89rem;
      margin-bottom: 0.06rem;
      box-shadow: 0 1px 0px rgba(150, 150, 150, 0.1);
    }
  }

  .div-empty {
    .icon-meiyougengduole {
      font-size: 1.3rem;
      color: #595656;
    }

    .empty-tip {
      margin-top: 0.08rem;
      font-size: 0.15rem;
      text-align: center;
      color: #595656;
      letter-spacing: 0.01rem;
    }
  }
}
</style>
